package list

type List struct {
	Head *Node
	Tail *Node
	Size int
}

type Node struct {
	Val  interface{}
	Next *Node
	Prev *Node
}

func NewList() *List {
	return &List{Head: nil, Tail: nil, Size: 0}
}

func (list *List) Append(node *Node) {
	if list.Size == 0 {
		list.Head = node
	}
	list.Tail = node
	list.Size++
}

func (list *List) Remove(index int) {
	current := list.Head
	for i := 0; i < index; i++ {
		current = current.Next
	}

	current.Prev.Next = current.Next

}

func (list *List) Pop() *Node {
	if list.Size == 0 {
		return nil
	}

	tmpNode := list.Head
	list.Head = list.Head.Next
	return tmpNode
}

func (list *List) MoveToFront(node *Node) {
	if node.Prev == nil && node.Next != nil {
		//在第一位
	} else if node.Next == nil && node.Prev != nil {
		//在最后一位
		node.Prev.Next = nil

	} else {
		//在中间
		node.Prev.Next = node.Next
		node.Next = list.Head
		list.Head = node
	}
}
